// pages/route/bus.js
const amap = require('../../utils/amap-config')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        selectType: "最便捷"
    },
    // 跳转页面
    changeNav: function (e) {
        // console.log(e)
        wx.navigateTo({
          url: '/pages/route/'+e.currentTarget.dataset.type+'?poilocation='+this.data.poilocation,
        })
    },
    transferOpts: function() {
        var that = this
        var itemList = ['最便捷', '最经济', '少换乘', '少步行', '不乘地铁']
        wx.showActionSheet({
            itemList: itemList,
            success: function (res) {
                // console.log(res.tapIndex)
                // 修改不乘地铁的index
                var type = (res.tapIndex === 4)? (res.tapIndex+ 1) : res.tapIndex
                // 调用换乘函数
                that.getbusRiding(type)
                that.setData({
                  selectType: itemList[res.tapIndex]
                })
            },
            fail (res) {
              console.log(res)
            }
          })
    },
    getbusRiding: function (type) {
        var that = this
        amap.map.getTransitRoute({
            origin: wx.getStorageSync('longitude')+ "," +wx.getStorageSync('latitude'),
            destination: that.data.poilocation,
            city: that.data.city,
            strategy: type,
            success: function (res) {
                // console.log(res)
                if (res && res.transits) {
                    var transits = res.transits
                    for (let i = 0; i<transits.length; i++){
                        var segments =  transits[i].segments
                        transits[i].transports = []
                        for (let j = 0; j<segments.length; j++ ){
                            if (segments[j].bus
                                && segments[j].bus.buslines
                                && segments[j].bus.buslines[0]
                                && segments[j].bus.buslines[0].name
                                ){
                                    var name = segments[j].bus.buslines[0].name
                                    if (j !== 0 ){
                                        name: '->' + name
                                    }
                                    transits[i].transports.push(name)
                                }
                        }
                        transits[i].transportname = transits[i].transports.join('')
                        transits[i].duration = parseInt(transits[i].duration / 60)
                        transits[i].distance = parseFloat(transits[i].distance / 1000).toFixed(2)
                    }
                }
                that.setData({
                transits: transits
                })
            },
            fail: function (info) {
                console.log(info)
            }
        })
    },
    

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // console.log(options)
        const that = this
        if (options.poilocation !== ''){
            // 设置/修改数据
            that.setData({
                poilocation: options.poilocation,
                city: wx.getStorageSync('city'),
                markers: [
                    {
                        id: 0,
                        longitude: wx.getStorageSync('longitude'),
                        latitude: wx.getStorageSync('latitude'),
                        iconPath:'../../static/images/nav_s.png',
                        width: 40,
                        height: 40,
                    },
                    {
                        id: 1,
                        longitude: parseFloat(options.poilocation.split(',')[0]),
                        latitude: parseFloat(options.poilocation.split(',')[1]),
                        iconPath:'../../static/images/nav_e.png',
                        width: 40,
                        height: 40,
                    }
                ]
            })
            // 调用获取高德地图公交线路的信息
            that.getbusRiding(0)
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})